//
//  HYOUSearchViewController.m
//  BestGift
//
//  Created by 温晓宇 on 16/1/7.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "HYOUSearchViewController.h"
#import "HYOUSearchView.h"
#import "HYOUSearchDetailViewController.h"
@interface HYOUSearchViewController ()<HYOUSearchViewDelegate>

@property(nonatomic, retain)HYOUSearchView *search;

@property(nonatomic, retain) NSString *string;

@property(nonatomic, retain) NSMutableArray *searchArray;

@property(nonatomic, retain) NSMutableArray *allArray;
@end

@implementation HYOUSearchViewController
//-(void)dealloc{
//    [_search release];
//    [_string release];
//    [_searchArray release];
//    [_allArray release];
//    [super dealloc];
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.search = [[HYOUSearchView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.search.backgroundColor = [UIColor whiteColor];
    self.search.delegate = self;
    [self.view addSubview:self.search];
    [_search release];
    
    self.searchArray = [NSMutableArray array];
    self.allArray = [NSMutableArray array];
    NSString *url = @"http://api.liwushuo.com/v2/search/hot_words";
    [AppTools finalAFNWithURLString:url parameters:nil successAFNBlock:^(id resuposeObject) {
        NSMutableDictionary *bigDic = resuposeObject;
        NSMutableDictionary *data = [bigDic objectForKey:@"data"];
        NSMutableArray *hot = [data objectForKey:@"hot_words"];
        for (NSString *str in hot) {
            [self.allArray addObject:str];
            NSLog(@"%@",self.allArray);
        }
        self.search.stringArray = self.allArray;
        NSLog(@"%@",self.allArray);
        
    }];
}
-(void)searchString:(NSString *)search{
    [self.searchArray removeAllObjects];
        NSString *url = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/search/item?keyword=%@&limit=20&offset=0&sort=",search];
        url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        NSLog(@"%@", url);
        [AppTools finalAFNWithURLString:url parameters:nil successAFNBlock:^(id resuposeObject) {
            NSMutableDictionary *bigDic = resuposeObject;
            NSMutableDictionary *data = [bigDic objectForKey:@"data"];
            NSMutableArray *items = [data objectForKey:@"items"];
            for (NSDictionary *dic in items) {
                HYOUHotModel *model = [[HYOUHotModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.searchArray addObject:model];
//                NSLog(@"%@",model.id);
//                [model release];
            }
            HYOUSearchDetailViewController *detail = [[HYOUSearchDetailViewController alloc]init];
            [self.navigationController pushViewController:detail animated:YES];
            detail.allArray = self.searchArray;
//            [detail release];
        
        }];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.search.searchController.active) {
        self.search.searchController.active = NO;
        [self.search.searchController.searchBar removeFromSuperview];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
